﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Assistant;
using App.Bus;

namespace App.admin
{
    public partial class addincome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session[EnumCollections.SESSION_LOGINNAME] != null && !String.IsNullOrWhiteSpace(Convert.ToString(Session[EnumCollections.SESSION_LOGINNAME])))
                {
                    BindConsumeAccounts();
                }
                else
                {
                    Response.Redirect("~/Login.aspx?request=" + Guid.NewGuid().ToString("N"));
                }
            }
        }

        private void BindConsumeAccounts()
        {
            var ds = DbHelperSQLite.Query("SELECT Id,AccountName FROM Account");
            this.ddlAccount.DataValueField = "Id";
            this.ddlAccount.DataTextField = "AccountName";
            this.ddlAccount.DataSource = ds.Tables[0];
            this.ddlAccount.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtIncomeDateTime.Value) || String.IsNullOrEmpty(this.txtIncome.Text))
            {
                Assistant.AlertBox.MessageBox("充值日期和充值金额不允许为空！");
                return;
            }
            DateTime incomeTime = DateTime.Parse(this.txtIncomeDateTime.Value);
            int incomeMoney = Convert.ToInt32(decimal.Parse(this.txtIncome.Text) * 100);
            int accountId = int.Parse(this.ddlAccount.SelectedValue);
            if (incomeMoney <= 0)
            {
                Assistant.AlertBox.MessageBox("充值金额必须大于0！");
                return;
            }


            try
            {
                Income income = new Income();
                income.AccountId = accountId;
                income.InsertDateTime = incomeTime;
                income.SaveMoney = incomeMoney;
                income.Add();
                AlertBox.MessageBoxThenUrl("操作成功！", "../default.aspx");
            }
            catch (Exception err)
            {
                AlertBox.MessageBox("保存出错：" + err.Message);
            }

        }
    }
}